﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weight.Base.Utility
{
    public  class MathHelper
    {
        /// <summary>
        /// 截取指定长度的小数位
        /// </summary>
        /// <param name="val">运算的值</param>
        /// <param name="decimials">保留的小数位数</param>
        /// <returns></returns>
        public static double GetFloorVal(double val,int decimials)
        {
            ///取整
            double num = Math.Truncate(val);
            string strVal = Convert.ToString(val);
            //判断小数的长度是否小于保留的小数位，如果小于直接返回
            int index = strVal.IndexOf(".");
            if (index == -1) return val;
            int len = strVal.Length - index;
            if (len <= decimials) return val;

            //截取小数位
            string tmp = strVal.Substring(index+1,decimials);
            string resVal = num + "." + tmp;
            double tmpVal = 0;
            double.TryParse(resVal,out tmpVal);
            return tmpVal;


        }
    }
}
